import axios from "axios";
import wx from "weixin-js-sdk";

export const wxShare = (
  title: string,
  link: string,
  timeLine: string,
  intro: string,
  image: string
) => {
  const canShare = true;

  const ua = window.navigator.userAgent.toLowerCase();
  if (/micromessenger/.test(ua)) {
    let newsTitle = title;
    let newsLink = link;
    let newsTimeline = timeLine;
    let newsIntro = intro;
    let newsImage = image;
    const url =
        "https://ssohudong.cztv.com/weixin_share/share?channel_id=1&url=" +
        encodeURIComponent(location.href.split("#")[0]) +
        "&callback=?";
    axios.get(url).then((msg: any) => {
      //   返回值是 '?({})' 的格式
      const data = msg.data;
      console.log("share:", data);
      const menuList = [
        // 'menuItem:openWithQQBrowser',
        // 'menuItem:openWithSafari',
        "menuItem:share:qq",
        "menuItem:share:weiboApp",
        "menuItem:share:QZone",

        "menuItem:copyUrl",
        "menuItem:editTag",
        "menuItem:delete",
        "menuItem:originPage",
        "menuItem:readMode",
        "menuItem:share:email",
        "menuItem:share:brand",
      ]; // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮
      if (data.code == 200) {
        wx.config({
          debug: false,
          appId: data.data.appId,
          timestamp: data.data.timestamp,
          nonceStr: data.data.nonceStr,
          signature: data.data.signature,
          jsApiList: [
            "onMenuShareTimeline",
            "onMenuShareAppMessage",
            "hideMenuItems",
            "showMenuItems",
            "openAddress",
            "updateTimelineShareData",
            "updateAppMessageShareData",
          ],
          openTagList: ["wx-open-launch-app"],
        });
      }
      wx.ready(function () {
        console.log(newsTimeline, newsIntro, newsLink, newsImage);
        if (!canShare) {
          const newMenuList = [
            ...menuList,
            "menuItem:share:appMessage",
            "menuItem:share:timeline",
          ];
          wx.hideMenuItems({
            menuList: newMenuList,
          });
        } else {
          wx.showMenuItems({
            menuList: ["menuItem:share:appMessage", "menuItem:share:timeline"],
          });
          wx.hideMenuItems({
            menuList: menuList,
          });
        }

        wx.onMenuShareTimeline({
          title: newsTitle,
          desc: newsIntro,
          link: newsLink,
          imgUrl: newsImage,
          success: function () {
            // 用户点击了分享后执行的回调函数
          },
        });

        // wx.updateAppMessageShareData({
        //   title: newsTimeline,
        //   desc: newsIntro,
        //   link: newsLink,
        //   imgUrl: newsImage,
        //   success: function () {
        //     // 设置成功
        //   }
        // })

        wx.onMenuShareAppMessage({
          title: newsTitle,
          desc: newsIntro,
          link: newsLink,
          imgUrl: newsImage,
          success: function () {
            // 用户点击了分享后执行的回调函数
          },
        });
        wx.onMenuShareQQ({
          title: newsTitle,
          desc: newsIntro,
          link: newsLink,
          imgUrl: newsImage,
          success: function () {
            // 用户点击了分享后执行的回调函数
          },
        });
        wx.onMenuShareWeibo({
          title: newsTitle,
          desc: newsIntro,
          link: newsLink,
          imgUrl: newsImage,
          success: function () {
            // 用户点击了分享后执行的回调函数
          },
        });
      });
    });
  }
};
